package com.hugh.emojirain;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AppCompatActivity {
    private View mContentView;
    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */

    private final static int EMOJI_COUNT = 15;

    private int screenWidth;

    private int screenHeight;

    private RelativeLayout frameLayout;

    private RelativeLayout.LayoutParams layoutParams;

    private ImageView view;

    private List<ImageView> views = new ArrayList<>();

    private List<Float> viewsY = new ArrayList<>();

    private static final int[] EmojiResArray = {
            R.drawable.d_aini,
            R.drawable.d_aoteman,
            R.drawable.d_baibai,
            R.drawable.d_beishang,
            R.drawable.d_bishi,
            R.drawable.d_bizui,
            R.drawable.d_chanzui,
            R.drawable.d_chijing,
            R.drawable.d_dahaqi,
            R.drawable.d_dalian,
            R.drawable.d_ding,
            R.drawable.d_doge,
            R.drawable.d_feizao,
            R.drawable.d_ganmao,
            R.drawable.d_guzhang,
            R.drawable.d_haha,
            R.drawable.d_haixiu,
            R.drawable.d_han,
            R.drawable.d_hehe,
            R.drawable.d_heixian,
            R.drawable.d_heng,
            R.drawable.d_huaxin,
            R.drawable.d_jiyan,
            R.drawable.d_keai,
            R.drawable.d_kelian,
            R.drawable.d_ku,
            R.drawable.d_kun,
            R.drawable.d_landelini,
            R.drawable.d_lei,
            R.drawable.d_madaochenggong,
            R.drawable.d_miao,
            R.drawable.d_nanhaier,
            R.drawable.d_nu,
            R.drawable.d_numa,
            R.drawable.d_nvhaier,
            R.drawable.d_qian,
            R.drawable.d_qinqin,
            R.drawable.d_shayan,
            R.drawable.d_shengbing,
            R.drawable.d_shenshou,
            R.drawable.d_shiwang,
            R.drawable.d_shuai,
            R.drawable.d_shuijiao,
            R.drawable.d_sikao,
            R.drawable.d_taikaixin,
            R.drawable.d_touxiao,
            R.drawable.d_tu,
            R.drawable.d_tuzi,
            R.drawable.d_wabishi,
            R.drawable.d_weiqu,
            R.drawable.d_xiaoku,
            R.drawable.d_xiongmao,
            R.drawable.d_xixi,
            R.drawable.d_xu,
            R.drawable.d_yinxian,
            R.drawable.d_yiwen,
            R.drawable.d_youhengheng,
            R.drawable.d_yun,
            R.drawable.d_zhajipijiu,
            R.drawable.d_zhuakuang,
            R.drawable.d_zhutou,
            R.drawable.d_zuiyou,
            R.drawable.d_zuohengheng,
            R.drawable.f_geili,
            R.drawable.f_hufen,
            R.drawable.f_jiong,
            R.drawable.f_meng,
            R.drawable.f_shenma,
            R.drawable.f_v5,
            R.drawable.f_xi,
            R.drawable.f_zhi,
            R.drawable.h_buyao,
            R.drawable.h_good,
            R.drawable.h_haha,
            R.drawable.h_lai,
            R.drawable.h_ok,
            R.drawable.h_quantou,
            R.drawable.h_ruo,
            R.drawable.h_woshou,
            R.drawable.h_ye,
            R.drawable.h_zan,
            R.drawable.h_zuoyi,
            R.drawable.l_shangxin,
            R.drawable.l_xin,
            R.drawable.o_dangao,
            R.drawable.o_feiji,
            R.drawable.o_ganbei,
            R.drawable.o_huatong,
            R.drawable.o_lazhu,
            R.drawable.o_liwu,
            R.drawable.o_lvsidai,
            R.drawable.o_weibo,
            R.drawable.o_weiguan,
            R.drawable.o_yinyue,
            R.drawable.o_zhaoxiangji,
            R.drawable.o_zhong,
            R.drawable.w_fuyun,
            R.drawable.w_shachenbao,
            R.drawable.w_taiyang,
            R.drawable.w_weifeng,
            R.drawable.w_xianhua,
            R.drawable.w_xiayu,
            R.drawable.sit,
            R.drawable.siu,
            R.drawable.siv,
            R.drawable.w_yueliang,
            R.drawable.d_hongbao1,
            R.drawable.d_hongbao2,
            R.drawable.d_hongbao3
    };

    private static final String[] EmojiTextArray = {
            "[爱你]",
            "[奥特曼]",
            "[拜拜]",
            "[悲伤]",
            "[鄙视]",
            "[闭嘴]",
            "[馋嘴]",
            "[吃惊]",
            "[哈欠]",
            "[打脸]",
            "[顶]",
            "[doge]",
            "[肥皂]",
            "[感冒]",
            "[鼓掌]",
            "[哈哈]",
            "[害羞]",
            "[汗]",
            "[微笑]",
            "[黑线]",
            "[哼]",
            "[色]",
            "[挤眼]",
            "[可爱]",
            "[可怜]",
            "[酷]",
            "[困]",
            "[白眼]",
            "[泪]",
            "[马到成功]",
            "[喵喵]",
            "[男孩儿]",
            "[怒]",
            "[怒骂]",
            "[女孩儿]",
            "[钱]",
            "[亲亲]",
            "[傻眼]",
            "[生病]",
            "[草泥马]",
            "[失望]",
            "[衰]",
            "[睡]",
            "[思考]",
            "[太开心]",
            "[偷笑]",
            "[吐]",
            "[兔子]",
            "[挖鼻]",
            "[委屈]",
            "[笑cry]",
            "[熊猫]",
            "[嘻嘻]",
            "[嘘]",
            "[阴险]",
            "[疑问]",
            "[右哼哼]",
            "[晕]",
            "[炸鸡啤酒]",
            "[抓狂]",
            "[猪头]",
            "[最右]",
            "[左哼哼]",
            "[给力]",
            "[互粉]",
            "[囧]",
            "[萌]",
            "[神马]",
            "[威武]",
            "[喜]",
            "[织]",
            "[NO]",
            "[good]",
            "[haha]",
            "[来]",
            "[OK]",
            "[拳头]",
            "[弱]",
            "[握手]",
            "[耶]",
            "[赞]",
            "[作揖]",
            "[伤心]",
            "[心]",
            "[蛋糕]",
            "[飞机]",
            "[干杯]",
            "[话筒]",
            "[蜡烛]",
            "[礼物]",
            "[绿丝带]",
            "[围脖]",
            "[围观]",
            "[音乐]",
            "[照相机]",
            "[钟]",
            "[浮云]",
            "[沙尘暴]",
            "[太阳]",
            "[微风]",
            "[鲜花]",
            "[下雨]",
            "[宝箱到]",
            "[想要宝物么]",
            "[我没带宝物呦]",
            "[月亮]",
            "[惊不惊喜]",
            "[刺不刺激]",
            "[心情真好]"
    };

    private static final String[] wait_string = {
            "衣带渐宽终不悔\n为伊消得人憔悴\n－柳永《蝶恋花》",
            "死生契阔\n与子成说\n执子之手\n与子偕老\n－佚名《诗经邶风击鼓》",
            "两情若是久长时\n又岂在朝朝暮暮\n－秦观《鹊桥仙》",
            "相思相见知何日?此时此夜难为情\n－李白《三五七言》",
            "有美人兮\n见之不忘\n一日不见兮\n思之如狂\n－佚名《凤求凰琴歌》",
            "这次我离开你\n是风\n是雨\n是夜晚；你笑了笑\n我摆一摆手\n一条寂寞的路便展向两头了\n－郑愁予《赋别》",
            "入我相思门\n知我相思苦\n长相思兮长相忆\n短相思兮无穷极\n－李白《三五七言》",
            "曾经沧海难为水\n除却巫山不是云\n－元稹《离思五首其四》",
            "君若扬路尘\n妾若浊水泥\n浮沈各异势\n会合何时谐?－曹植《明月上高楼》",
            "凄凉别后两应同\n最是不胜清怨月明中\n－纳兰性德《虞美人》",
            "还君明珠双泪垂\n恨不相逢未嫁时\n－张籍《节妇吟》",
            "如何让你遇见我\n在我最美丽的时刻\n为这\n我已在佛前求了五百年\n求他让我们结一段尘缘\n－席慕蓉《一棵开花的树》",
            "嗟余只影系人间\n如何同生不同死?－陈衡恪《题春绮遗像》",
            "相思树底说相思\n思郎恨郎郎不知\n－梁启超《台湾竹枝词》",
            "自君之出矣\n明镜暗不治\n思君如流水\n何有穷已时\n－徐干《室思》",
            "相见争如不见\n有情何似无情\n－司马光《西江月》",
            "落红不是无情物\n化作春泥更护花\n－龚自珍《己亥杂诗》",
            "天不老\n情难绝\n心似双丝网\n中有千千结\n－张先《千秋岁》",
            "似此星辰非昨夜\n为谁风露立中宵\n－黄景仁《绮怀诗二首其一》",
            "直道相思了无益\n未妨惆怅是清狂\n－李商隐《无题六首其三》",
            "深知身在情长在\n怅望江头江水声\n－李商隐《暮秋独游曲江》",
            "人到情多情转薄\n而今真个不多情\n－纳兰性德《摊破浣溪沙》",
            "此情可待成追忆\n只是当时已惘然\n－李商隐《锦瑟》",
            "相思***多少\n地角天涯未是长\n－张仲素《燕子楼》",
            "问世间\n情是何物\n直教生死相许\n－元好问《摸鱼儿二首其一》",
            "身无彩凤双飞翼\n心有灵犀一点通\n－李商隐《无题》",
            "兽炉沈水烟\n翠沼残花片\n一行行写入相思传\n－张可久《塞鸿秋》",
            "平生不会相思\n才会相思\n便害相思\n－徐再思《折桂令》",
            "一寸相思千万绪\n人间没个安排处\n－李冠《蝶恋花》",
            "十年生死两茫茫\n不思量\n自难忘\n千里孤坟\n无处话凄凉\n－苏轼《江城子》",
            "今夕何夕\n见此良人\n－佚名《诗经唐风绸缪》",
            "天长地久有时尽\n此恨绵绵无绝期\n－白居易《长恨歌》",
            "在天愿作比翼鸟\n在地愿为连理枝\n－白居易《长恨歌》",
            "重叠泪痕缄锦字\n人生只有情难死\n－文廷式《蝶恋花》",
            "一个是阆苑仙葩\n一个是美玉无瑕\n若说没奇缘\n今生偏又遇著他；若说有奇缘\n如何心事终虚话\n－曹雪芹《枉凝眉》",
            "春蚕到死丝方尽\n蜡炬成灰泪始乾\n－李商隐《无题》",
            "他生莫作有情痴\n人间无地著相思\n－况周颐《减字浣溪沙》",
            "尊前拟把归期说\n未语春容先惨咽\n－欧阳修《玉楼春》",
            "关关雎鸠\n在河之洲\n窈宨淑女\n君子好逑\n－佚名《诗经周南关雎》",
            "人生自是有情痴\n此恨不关风与月\n－欧阳修《玉楼春》",
            "此去经年\n应是良辰好景虚设\n便纵有\n千种风情\n更与何人说\n－柳永《雨霖铃》",
            "只愿君心似我心\n定不负相思意\n－李之仪《卜算子》",
            "落花人独立\n微雨燕双飞\n－晏几道《临江仙二首其二》",
            "可怜无定河边骨\n犹是春闺梦裏人\n－陈陶《陇西行》",
            "千金纵买相如赋\n脉脉此情谁诉\n－辛弃疾《摸鱼儿》",
            "梧桐树\n三更雨\n不道离情正苦\n一叶叶\n一声声\n空阶滴到明\n－温庭筠《更漏子三首其三》",
            "结发为夫妻\n恩爱两不疑\n－苏武《结发为夫妻》",
            "寻好梦\n梦难成\n况谁知我此时情\n枕前泪共帘前雨\n隔个窗儿滴到明\n－聂胜琼《鹧鸪天》",
            "一场寂寞凭谁诉\n算前言\n总轻负\n－柳永《昼夜乐》",
            "天长路远魂飞苦\n梦魂不到关山难\n长相思\n摧心肝\n－《李白长相思二首其一》",
            "不要因为也许会改变\n就不肯说那句美丽的誓言\n不要因为也许会分离\n就不敢求一次倾心的相遇\n－席慕蓉《印记》",
            "愿我如星君如月\n夜夜流光相皎洁\n－范成大《车遥遥篇》",
            "鱼沈雁杳天涯路\n始信人间别离苦\n－戴叔伦《相思曲》",
            "执手相看泪眼\n竟无语凝噎\n－柳永《雨霖铃》",
            "诚知此恨人人有\n贫贱夫妻百事哀\n－元稹《遣悲怀三首其二》",
            "换我心\n为你心\n始知相忆深\n－顾敻《诉衷情》",
            "忆君心似西江水\n日夜东流无歇时\n－鱼玄机《江陵愁望有寄》",
            "春心莫共花争发\n一寸相思一寸灰\n－李商隐《无题六首其六》",
            "相思似海深\n旧事如天远\n－乐婉《卜算子》",
            "相恨不如潮有信\n相思始觉海非深\n－白居易《浪淘沙》",
            "忍把千金酬一笑?毕竟相思\n不似相逢好\n－邵瑞彭《蝶恋花》",
            "天涯地角有穷时\n只有相思无尽处\n－晏殊《玉楼春》",
            "都道是金玉良缘\n俺只念木石前盟\n空对著\n山中高士晶莹雪；终不忘\n世外仙姝寂寞林\n－曹雪芹《终身误》",
            "人如风后入江云\n情似雨馀黏地絮\n－周邦彦《玉楼春》",
            "滴不尽相思血泪抛红豆\n开不完春柳春花满画楼\n－曹雪芹《红豆词》",
            "泪眼问花花不语\n乱红飞过秋千去\n－欧阳修《蝶恋花二首其一》",
            "瘦影自怜秋水照\n卿须怜我我怜卿\n－冯小青《怨》",
            "锺情怕到相思路\n盼长堤\n草尽红心\n动愁吟\n碧落黄泉\n两处难寻\n－朱彝尊《高阳台》",
            "若问闲情都几许?一川烟草\n满城风絮\n梅子黄时雨\n－贺铸《横塘路》",
            "多情只有春庭月\n犹为离人照落花\n－张泌《寄人》",
            "花红易衰似郎意\n水流无限似侬愁\n－刘禹锡《竹枝词四首其二》",
            "若有知音见采\n不辞遍唱阳春\n－晏殊《山亭柳赠歌者》",
            "明月楼高休独倚\n酒入愁肠\n化作相思泪\n－范仲淹《苏幕遮》",
            "思君如明烛\n煎心且衔泪\n－陈叔达《自君之出矣》",
            "生当复来归\n死当长相思\n－苏武《结发为夫妻》",
            "休言半纸无多重\n万斛离愁尽耐担\n－陈蓬姐《寄外二首其二》",
            "妾似胥山长在眼\n郎如石佛本无心\n－朱彝尊《鸳鸯湖棹歌》",
            "不知魂已断\n空有梦相随\n除却天边月\n没人知\n－韦庄《女冠子二首其一》",
            "同心而离居\n忧伤以终老\n－佚名《涉江采芙蓉》",
            "夜月一帘幽梦\n春风十里柔情\n－秦观《八六子》",
            "从别后\n忆相逢\n几回魂梦与君同\n－晏几道《鹧鸪天三首其一》",
            "多情自古伤离别\n更那堪\n冷落清秋节\n－柳永《雨霖铃》",
            "无情不似多情苦\n一寸还成千万缕\n－晏殊《玉楼春》",
            "唯将终夜长开眼\n报答平生未展眉\n－元稹《遣悲怀三首其三》",
            "别来半岁音书绝\n一寸离肠千万结\n－韦庄《应天长》",
            "相思一夜梅花发\n忽到窗前疑是君\n－卢仝《有所思》",
            "直缘感君恩爱一回顾\n使我双泪长珊珊\n－卢仝《楼上女儿曲》",
            "相思本是无凭语\n莫向花牋费泪行\n－晏几道《鹧鸪天三首其二》",
            "离愁渐远渐无穷\n迢迢不断如春水\n－欧阳修《踏莎行》",
            "若教眼底无离恨\n不信人间有白头\n－辛弃疾《鹧鸪天》",
            "开辟鸿蒙\n谁为情种?都只为风月情浓\n－曹雪芹《红楼梦引子》",
            "明月不谙离恨苦\n斜光到晓穿朱户\n－晏殊《鹊踏枝》",
            "临别殷勤重寄词\n词中有誓两心知\n－白居易《长恨歌》",
            "泪纵能乾终有迹\n语多难寄反无词\n－陈端生《寄外》",
            "还卿一钵无情泪\n恨不相逢未剃时\n－苏曼殊《本事诗》",
            "终日两相思\n为君憔悴尽\n百花时\n－温庭筠《南歌子四首其二》",
            "欲寄彩笺兼尺素\n山长水阔知何处\n－晏殊《鹊踏枝》",
            "离恨却如春草\n更行更远还生\n－李煜《清平乐》",
            "鸿雁在云鱼在水\n惆怅此情难寄\n－晏殊《清平乐二首其二"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fullscreen);
        frameLayout = (RelativeLayout) findViewById(R.id.fullscreen_content_layout);
        DisplayMetrics dm = getResources().getDisplayMetrics();


        screenWidth = dm.widthPixels;

        screenHeight = dm.heightPixels;
        mContentView = findViewById(R.id.fullscreen_content);

        // Set up the user interaction to manually show or hide the system UI.
        mContentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EmojiRain();
            }
        });

    }

    int viewcount = 0;

    private void EmojiRain(){
        final Random random = new Random(System.currentTimeMillis());
        if(viewcount >= 60){
            ((TextView)(mContentView)).setText(wait_string[random.nextInt(wait_string.length-1)]);
            ((TextView)(mContentView)).setTextSize(25);
            return;
        }


        views.clear();
        int icon_count = EmojiResArray.length;
        int icon_id =  random.nextInt(icon_count);
        int big_icon = 0;
        if(icon_id <= icon_count && icon_id >= icon_count-2){
            big_icon = icon_id;
            icon_id = icon_count;
        }
        for(int i = 0;i<big_icon;i++){
            addView(icon_count-2);
        }
        for(int i = 0; i< EMOJI_COUNT - big_icon; i++){
            addView(icon_id);
        }

        for(int i = 0; i< EMOJI_COUNT; i++){
            final ImageView view = views.get(i);
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "translationY",viewsY.get(i), (float)(screenHeight)).setDuration(new Random().nextInt(3500)+3000);
            objectAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    frameLayout.removeView(view);
                    viewcount--;
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            objectAnimator.start();
        }
        ((TextView)(mContentView)).setText(EmojiTextArray[icon_id]);((TextView)(mContentView)).setTextSize(50);
    }

    public static int dip2px(Context context, float dpValue){

        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public int getEmojiId(int icon_id){
        return icon_id >= EmojiResArray.length ? EmojiResArray[0]:EmojiResArray[icon_id];
    }

    public void addView(int icon_id){
        view = new ImageView(FullscreenActivity.this);
        int emoji_id = getEmojiId(icon_id);

        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources( ),emoji_id);
        view.setImageResource(emoji_id);
        layoutParams  = new RelativeLayout.LayoutParams((int)(bitmap.getWidth()/1.5f),(int)(bitmap.getHeight()/1.5f));
        frameLayout.addView(view,1, layoutParams);
        view.setY(-(new Random().nextInt(1200) + bitmap.getHeight()*2));
        view.setX(new Random().nextInt(screenWidth - dip2px(FullscreenActivity.this, 100) - 10) + 10);
        view.setZ(new Random().nextInt(10) + 10);

        views.add(view);
        viewsY.add(view.getY());
        viewcount++;
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }
}
